package com.zis.system.server;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.system.dao.DictTypeDao;
import com.zis.system.model.DictionaryType;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.system.dao.DictDao;
import com.zis.system.inter.IDictionaryInter;
import com.zis.system.model.Dictionary;
import com.zis.system.model.Sys;

/**
 * 数据字典类型
 * Create by wangshen 2018/1/31
 */
@Service
@Transactional("txManager")
public class DictInterImpl implements IDictionaryInter {

    @Autowired
    private DictDao dictDao;

    @Autowired
    private DictTypeDao dictTypeDao;

    @Override
    public ResponseEntity insert(Dictionary model) throws Exception {
        int num = dictDao.getSQLManager().insert(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity update(Dictionary model) throws Exception {
        int num = dictDao.getSQLManager().updateTemplateById(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity delete(String id) throws Exception {
        int num = dictDao.getSQLManager().deleteById(Dictionary.class, id);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity selectById(String id) throws Exception {
        Dictionary result = dictDao.unique(id);
        if (result != null) {
            return ResponseEntity.success(JSON.parseObject(JSON.toJSONString(result)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    @Override
    public PageQuery<Dictionary> findByPage(SearchParams params) throws Exception {
        PageQuery<Dictionary> query = new PageQuery<>();
        query.setPageNumber(params.getPage());
        query.setPageSize(params.getPageSize());
        query.setParas(params.getParams());
        dictDao.getSQLManager().pageQuery("system.dictionary.query", Dictionary.class, query);
        return query;
    }

    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        List<Dictionary> resultList = dictDao.getSQLManager().select("system.dictionary.query", Dictionary.class, params);
        if (resultList != null && resultList.size() > 0) {
            return ResponseEntity.success(JSON.parseArray(JSON.toJSONString(resultList)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    /**
     * 查询全部数据字典转为json
     * @return
     */
    @Override
    public ResponseEntity findAllToJson() throws Exception {
        JSONObject result = new JSONObject();
        JSONObject dictObj = new JSONObject();
        JSONObject dictArray = new JSONObject();
        List<DictionaryType> dictTypeList = dictTypeDao.all();
        for (DictionaryType dictType : dictTypeList){
            //组织封装明细
            JSONObject dictJson = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            Dictionary params = new Dictionary();
            params.setDictTypeCode(dictType.getDictTypeCode());
            List<Dictionary> dictList = dictDao.template(params);
            for (Dictionary dict : dictList){
                dictJson.put(dict.getDictValue(),dict.getDictName());
                JSONObject temp = new JSONObject();
                temp.put("value", dict.getDictValue());
                temp.put("label", dict.getDictName());
                jsonArray.add(temp);
            }
            dictObj.put(dictType.getDictTypeCode(),dictJson);//对象
            dictArray.put(dictType.getDictTypeCode(),jsonArray);//数组对象
        }
        result.put("dictJSON",dictObj);
        result.put("dictArray",dictArray);
        return ResponseEntity.success(result);
    }
}
